class ChatUIController {
    constructor() {
        this.elements = {
            messageContainer: document.getElementById('message-container'),
            messageInput: document.getElementById('message-input'),
            sendButton: document.getElementById('send-button'),
            fileButton: document.getElementById('file-button'),
            emojiButton: document.getElementById('emoji-button'),
            typingIndicator: document.getElementById('typing-indicator'),
            conversationList: document.getElementById('conversation-list'),
            userList: document.getElementById('user-list')
        };
        
        this.initEventListeners();
        this.initRightClickMenu();
    }
    
    // 初始化事件监听
    initEventListeners() {
        // 发送消息
        this.elements.sendButton.addEventListener('click', () => this.sendMessage());
        this.elements.messageInput.addEventListener('keypress', (e) => {
            if (e.key === 'Enter' && !e.shiftKey) {
                e.preventDefault();
                this.sendMessage();
            }
        });
        
        // 输入状态监测
        this.elements.messageInput.addEventListener('input', () => {
            this.handleTyping();
        });
        
        // 文件上传
        this.elements.fileButton.addEventListener('change', (e) => {
            this.handleFileUpload(e.target.files[0]);
        });
    }
    
    // 初始化右键菜单
    initRightClickMenu() {
        document.addEventListener('contextmenu', (e) => {
            const messageElement = e.target.closest('.message-item');
            if (messageElement) {
                e.preventDefault();
                this.showMessageContextMenu(e, messageElement);
            }
        });
        
        document.addEventListener('click', () => {
            this.hideContextMenu();
        });
    }
    
    // 显示消息上下文菜单
    showMessageContextMenu(event, messageElement) {
        this.hideContextMenu();
        
        const messageId = messageElement.dataset.messageId;
        const isOwnMessage = messageElement.classList.contains('own-message');
        const menuHtml = this.generateContextMenuHtml(messageId, isOwnMessage);
        
        const menu = document.createElement('div');
        menu.className = 'message-context-menu';
        menu.style.left = event.pageX + 'px';
        menu.style.top = event.pageY + 'px';
        menu.innerHTML = menuHtml;
        
        document.body.appendChild(menu);
        
        // 添加菜单项事件
        this.attachContextMenuEvents(menu, messageId, isOwnMessage);
    }
    
    // 生成上下文菜单HTML
    generateContextMenuHtml(messageId, isOwnMessage) {
        let menuItems = '';
        
        if (isOwnMessage) {
            menuItems += `<div class="menu-item" data-action="recall">撤回消息</div>`;
        }
        
        menuItems += `
            <div class="menu-item" data-action="reply">回复</div>
            <div class="menu-item" data-action="copy">复制</div>
            <div class="menu-item" data-action="delete">删除</div>
            <div class="menu-item" data-action="forward">转发</div>
        `;
        
        return `<div class="context-menu-content">${menuItems}</div>`;
    }
}